package com.yongcheon.starhealthcare.main.drawer;

import android.content.Context;
import android.content.Intent;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

import com.facebook.widget.LoginButton;
import com.yongcheon.starhealthcare.R;
import com.yongcheon.starhealthcare.friend.FriendActivity;
import com.yongcheon.starhealthcare.facebook.FacebookCallback;

import java.util.Arrays;

/**
 * Created by yongcheon on 2015-02-05.
 */
public class DrawerListeClickListener implements AdapterView.OnItemClickListener {
    Context context;

    public DrawerListeClickListener(Context context){
        this.context = context;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch(position){
            case DrawListItemFactory.INDEX_FRIEND :
                startFinendActivity(view);
            break;
            case DrawListItemFactory.INDEX_FACEBOOK :
                changeFacebookLongin(view);
            break;
        }

    }

    private void changeFacebookLongin(View view){
        if (FacebookCallback.session != null){
            if(FacebookCallback.session.isOpened()){
                FacebookCallback.session.close();
            }else if (FacebookCallback.session.isClosed()) {
                faceBookLogin(view);
            }
        }else{
            faceBookLogin(view);
        }
    }

    private void faceBookLogin(View view){
        DrawerLayout drawerLayout = (DrawerLayout)view.getParent().getParent();
        LoginButton loginButton = (LoginButton)drawerLayout.findViewById(R.id.authButton);
        loginButton.setReadPermissions(Arrays.asList("public_profile", "user_friends"));

        try {
            loginButton.callOnClick();
        }catch(Exception e){
            Log.i("=====================TEST> ", e.toString());
        }
    }

    private void startFinendActivity(View view){
//        DrawerLayout drawerLayout = (DrawerLayout)view.getParent().getParent();
//        ListView listView = (ListView)drawerLayout.findViewById(R.id.left_drawer);
//        drawerLayout.closeDrawer(listView);
        Intent intent = new Intent();
        intent.setClass(context, FriendActivity.class);
        context.startActivity(intent);
    }
}
